package com.guch.help.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.guch.help.R;
import com.guch.help.adapter.ActiveAdapter;
import com.guch.help.bean.ActiveBean;
import com.guch.help.databinding.ActivityActiveBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 激活页面
 */
public class ActiveActivity extends AppCompatActivity {

    private ActivityActiveBinding mBinding;
    private List<ActiveBean> mList;
    private ActiveAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_active);

        mBinding.rlTitle.ivBack.setOnClickListener(v -> finish());

        mBinding.rlTitle.tvTitle.setText(getResources().getString(R.string.app_active));

        getData();

        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        DividerItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider_inset));
        mBinding.recyclerView.addItemDecoration(itemDecoration);

        mAdapter = new ActiveAdapter(this, mList);
        mBinding.recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new ActiveAdapter.OnItemClickLister() {
            @Override
            public void onClick(View view, int position) {
                updateData(position);
            }
        });
    }

    private void updateData(int pos) {
        if(mList != null) {
            for(int i=0; i< mList.size(); i++) {
                mList.get(i).setSelected(i == pos);
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    private void getData() {
        mList = new ArrayList<>();
        mList.add(new ActiveBean(1, 5, true));
        mList.add(new ActiveBean(3, 10, false));
        mList.add(new ActiveBean(30, 20, false));
        mList.add(new ActiveBean(180, 60, false));
        mList.add(new ActiveBean(360, 100, false));
    }

}
